#!/system/bin/sh
# Project WIPE https://github.com/yc9559/cpufreq-interactive-opt
# Author: yc9559
# Generated at: Sat May  5 10:53:04 2018

# $1:value $2:file path
function set_value() {
	if [ -f $2 ]; then
		chown 0.0 $2
		chmod 0666 $2
		echo $1 > $2
		chmod 0444 $2
	fi
}

# $1:cpu0 $2:timer_rate $3:value
function set_param() {
	echo $3 > /sys/devices/system/cpu/$1/cpufreq/interactive/$2
}

action=$1
setenforce 0

# RunOnce
if [ ! -f /dev/project_wipe_runonce ]; then
	# make sure that sysfs is RW
	mount -o remount,rw sysfs /sys
fi

# disable hotplug to switch governor
set_value 0 /sys/module/msm_thermal/core_control/enabled

set_value 1 /sys/devices/system/cpu/cpu4/online
chown 0.0 /sys/devices/system/cpu/cpu0/cpufreq/interactive/*
chown 0.0 /sys/devices/system/cpu/cpu4/cpufreq/interactive/*
chmod 0666 /sys/devices/system/cpu/cpu0/cpufreq/interactive/*
chmod 0666 /sys/devices/system/cpu/cpu4/cpufreq/interactive/*

# RunOnce
if [ ! -f /dev/project_wipe_runonce ]; then
	# set flag
	touch /dev/project_wipe_runonce

	# Perfd, nothing to worry about, if error the script will continue
	stop perfd

	set_value 90 /proc/sys/kernel/sched_spill_load
	set_value 0 /proc/sys/kernel/sched_boost
	set_value 1 /proc/sys/kernel/sched_prefer_sync_wakee_to_waker
	set_value 40 /proc/sys/kernel/sched_init_task_load
	set_value 3000000 /proc/sys/kernel/sched_freq_inc_notify

	set_value 2-3 /dev/cpuset/background/cpus
	set_value 2-3 /dev/cpuset/system-background/cpus
	set_value 0-2,4-7 /dev/cpuset/foreground/cpus
	set_value 0-2,4-7 /dev/cpuset/top-app/cpus

	set_value 80 /sys/module/cpu_boost/parameters/input_boost_ms
	set_value 0 /sys/module/msm_performance/parameters/touchboost

	set_value 580000 /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
	set_value 1080000 /sys/devices/system/cpu/cpu4/cpufreq/scaling_min_freq

	# shared interactive parameters
	set_param cpu0 timer_rate 20000
	set_param cpu4 timer_rate 20000
	set_param cpu0 timer_slack 180000
	set_param cpu4 timer_slack 180000
	set_param cpu0 io_is_busy 0
	set_param cpu4 io_is_busy 0
	set_param cpu0 use_sched_load 1
	set_param cpu4 use_sched_load 1
	set_param cpu0 ignore_hispeed_on_notif 0
	set_param cpu4 ignore_hispeed_on_notif 0
	set_value 0 /sys/devices/system/cpu/cpu0/cpufreq/interactive/enable_prediction
	set_value 0 /sys/devices/system/cpu/cpu4/cpufreq/interactive/enable_prediction
fi

if [ "$action" = "powersave" ]; then
	set_value "0:1080000 4:0" /sys/module/cpu_boost/parameters/input_boost_freq
	set_param cpu0 boost 0
	set_param cpu4 boost 0
	set_param cpu0 above_hispeed_delay "18000 1580000:58000"
	set_param cpu0 hispeed_freq 1080000
	set_param cpu0 go_hispeed_load 98
	set_param cpu0 boostpulse_duration 18000
	set_param cpu0 target_loads "80 880000:63 1080000:83 1380000:76 1480000:98 1580000:85"
	set_param cpu0 min_sample_time 18000
	set_param cpu4 above_hispeed_delay "58000 1380000:18000 1680000:58000"
	set_param cpu4 hispeed_freq 1080000
	set_param cpu4 go_hispeed_load 86
	set_param cpu4 boostpulse_duration 58000
	set_param cpu4 target_loads "80 1380000:87 1680000:98"
	set_param cpu4 min_sample_time 18000
fi

if [ "$action" = "balance" ]; then
	set_value "0:1080000 4:0" /sys/module/cpu_boost/parameters/input_boost_freq
	set_param cpu0 boost 0
	set_param cpu4 boost 0
	set_param cpu0 above_hispeed_delay "38000"
	set_param cpu0 hispeed_freq 1080000
	set_param cpu0 go_hispeed_load 98
	set_param cpu0 boostpulse_duration 18000
	set_param cpu0 target_loads "80 880000:62 1380000:67 1480000:98"
	set_param cpu0 min_sample_time 38000
	set_param cpu4 above_hispeed_delay "58000 1380000:18000 1680000:58000"
	set_param cpu4 hispeed_freq 1080000
	set_param cpu4 go_hispeed_load 88
	set_param cpu4 boostpulse_duration 38000
	set_param cpu4 target_loads "80 1680000:98"
	set_param cpu4 min_sample_time 18000
fi

if [ "$action" = "performance" ]; then
	set_value "0:1080000 4:0" /sys/module/cpu_boost/parameters/input_boost_freq
	set_param cpu0 boost 0
	set_param cpu4 boost 0
	set_param cpu0 above_hispeed_delay "18000 1480000:58000 1580000:38000"
	set_param cpu0 hispeed_freq 1080000
	set_param cpu0 go_hispeed_load 97
	set_param cpu0 boostpulse_duration 78000
	set_param cpu0 target_loads "80 880000:76 1080000:82 1380000:68 1480000:74 1580000:79"
	set_param cpu0 min_sample_time 78000
	set_param cpu4 above_hispeed_delay "18000 1680000:58000"
	set_param cpu4 hispeed_freq 1380000
	set_param cpu4 go_hispeed_load 82
	set_param cpu4 boostpulse_duration 38000
	set_param cpu4 target_loads "80 1380000:68 1680000:98"
	set_param cpu4 min_sample_time 18000
fi

if [ "$action" = "fast" ]; then
	set_param cpu0 boost 1
	set_param cpu4 boost 1
	set_param cpu0 above_hispeed_delay "18000"
	set_param cpu4 above_hispeed_delay "18000 1980000:38000"
	set_param cpu0 hispeed_freq 1580000
	set_param cpu4 hispeed_freq 1580000
	set_param cpu0 target_loads "90"
	set_param cpu4 target_loads "90 1980000:95"
fi

chmod 0444 /sys/devices/system/cpu/cpu0/cpufreq/interactive/*
chmod 0444 /sys/devices/system/cpu/cpu4/cpufreq/interactive/*

exit 0